<template>
  <div class="min-h-screen bg-gray-100 py-8 px-4">
    <div class="max-w-md mx-auto bg-white shadow-lg rounded-lg p-6">
      <h1 class="text-2xl font-bold text-center mb-6">新建数据库</h1>

      <div class="mb-4">
        <label for="timestamp" class="block text-sm font-medium text-gray-700 mb-2">
          当前时间戳
        </label>
        <input
          type="text"
          id="timestamp"
          v-model="currentTime"
          disabled
          class="w-full px-4 py-2 border border-gray-300 rounded-md bg-gray-100 text-gray-700 cursor-not-allowed focus:outline-none"
        />
      </div>

      <button
        @click="createDatabase"
        :disabled="loading"
        class="w-full bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded-md transition duration-200 ease-in-out"
      >
        {{ loading ? '创建中…' : '新建数据库' }}
      </button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';

const currentTime = ref<string>(''); // 当前时间戳
const loading = ref<boolean>(false);
const error = ref<string | null>(null);

// 格式化日期
function formatTime(date: Date): string {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');

  return `${year}${month}${day}${hours}${minutes}${seconds}`;
}

// 定时更新当前时间
function updateCurrentTime(): void {
  const now = new Date();
  currentTime.value = formatTime(now);
}

onMounted(() => {
  updateCurrentTime();
  setInterval(updateCurrentTime, 1000);
});

// 创建数据库
async function createDatabase(): Promise<void> {
  loading.value = true;
  error.value = null;

  try {
    const response = await fetch('http://localhost:8080/db/create', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: new URLSearchParams({
        newName: 'db' + currentTime.value,
        fromDb: 'ghpt'
      })
    });

    if (!response.ok) throw new Error('网络响应失败');

    alert(`数据库 db${currentTime.value} 建立完成！`);
  } catch (err) {
    if (err instanceof Error) {
      error.value = err.message;
    } else {
      error.value = '创建数据库时出错';
    }
    alert(error.value);
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.min-h-screen {
  min-height: 100vh;
  background: linear-gradient(135deg, #e0f7fa, #e8eaf6);
  padding: 2rem 1rem;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.max-w-md {
  max-width: 28rem;
  background-color: #fff;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border-radius: 1rem;
  padding: 2rem;
  margin-top: 40px;
  transition: transform 0.2s ease;
}

.max-w-md:hover {
  transform: translateY(-3px);
}

h1 {
  font-size: 1.75rem;
  font-weight: 700;
  text-align: center;
  color: #333;
  margin-bottom: 1.5rem;
}

label {
  font-size: 0.875rem;
  font-weight: 500;
  color: #555;
  margin-bottom: 0.5rem;
  display: block;
}

input[type='text'] {
  width: 100%;
  padding: 0.5rem 1rem;
  border: 1px solid #ccc;
  border-radius: 0.375rem;
  background-color: #f5f5f5;
  color: #666;
  cursor: not-allowed;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
  transition: box-shadow 0.2s ease;
}

input[type='text']:hover {
  box-shadow: inset 0 1px 5px rgba(0, 0, 0, 0.1);
}

button {
  width: 100%;
  background: linear-gradient(135deg, #42a5f5, #1e88e5);
  color: #fff;
  font-weight: 600;
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
  transition: all 0.3s ease;
  border: none;
  cursor: pointer;
}

button:hover {
  background: linear-gradient(135deg, #1e88e5, #1565c0);
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
}

button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
</style>
